# -*- coding: utf-8 -*-

from resource_management import *
from resource_management.core.resources.system import Execute
from resource_management.core.source import *
from resource_management.core.resources.system import Directory,File
from resource_management.core.logger import Logger

def presto_env(presto_role):
    print("Presto设置开始")
    import params

    # 删除pid目录
    Directory([params.presto_pid_dir], action="delete")

    # 创建pid目录
    Directory([params.presto_log_dir, params.presto_pid_dir],
              mode=0755,
              cd_access='a',
              owner=params.presto_user,
              group=params.presto_group,
              create_parents=True
              )

    # 创建presto/etc目录
    Directory([params.presto_etc_dir],
              mode=0755,
              cd_access='a',
              owner=params.presto_user,
              group=params.presto_group,
              create_parents=True
              )

    # 创建presto/etc/catalog目录
    Directory([params.presto_etc_catalog_dir],
              mode=0755,
              cd_access='a',
              owner=params.presto_user,
              group=params.presto_group,
              create_parents=True
              )

    # 创建presto_data目录
    Directory([params.presto_data_dir],
              mode=0755,
              cd_access='a',
              owner=params.presto_user,
              group=params.presto_group,
              create_parents=True
              )

    # Ensure all files owned by presto user
    cmd = format("chown -R {presto_user}:{presto_group} {presto_home} {presto_log_dir} {presto_pid_dir} {presto_data_dir}")
    Execute(cmd)

    # 创建config.properties文件
    if presto_role == "coordinator":
        File(format(params.presto_etc_dir + "/config.properties"),
             mode=0755,
             content=Template("presto_coordinator_config.properties.j2"),
             owner=params.presto_user,
             group=params.presto_user
             )
    if presto_role == "worker":
        File(format(params.presto_etc_dir + "/config.properties"),
             mode=0755,
             content=Template("presto_worker_config.properties.j2"),
             owner=params.presto_user,
             group=params.presto_group
             )

    # 创建node.properties文件
    File(format(params.presto_etc_dir + "/node.properties"),
         mode=0755,
         content=Template("presto_node.properties.j2"),
         owner=params.presto_user,
         group=params.presto_group
         )

    # 创建jvm.config
    jvm_config = InlineTemplate(params.presto_jvm_config)
    File(format("{presto_etc_dir}/jvm.config"),
         owner=params.presto_user,
         group=params.presto_group,
         content=jvm_config
         )

    # 创建log.properties
    File(format(params.presto_etc_dir + "/log.properties"),
         mode=0755,
         content=Template("presto_log.properties.j2"),
         owner=params.presto_user,
         group=params.presto_group
         )

    # 创建connetcor.properties
    for key, value in params.presto_connector_properties.iteritems():
        File(format(params.presto_etc_catalog_dir + "/" + key),
             mode=0755,
             content=value,
             owner=params.presto_user,
             group=params.presto_group
             )

    Logger.info("Presto 设置结束")